package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsImportantSecurity;
import com.ruoyi.system.domain.news.dto.NewsImportantSecurityDto;
import com.ruoyi.system.domain.news.query.NewsImportantSecurityQuery;
import com.ruoyi.system.domain.news.vo.NewsImportantSecurityVo;
import com.ruoyi.system.service.news.NewsImportantSecurityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsImportantSecurity")
@Api(value = "newsImportantSecurityController", tags  = "信息安全国内外要闻")
public class NewsImportantSecurityController extends BaseController {

	@Resource
	private NewsImportantSecurityService newsImportantSecurityService;

    @ApiOperation(value = "新增/修改信息安全国内外要闻信息", notes = "新增/修改信息安全国内外要闻信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsImportantSecurityDto newsImportantSecurityDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsImportantSecurity newsImportantSecurity = new NewsImportantSecurity();
		BeanUtils.copyProperties(newsImportantSecurityDto,newsImportantSecurity);
		if (newsImportantSecurity.getId()==null) {
			newsImportantSecurity.setCreateBy(loginUser.getUsername());
			newsImportantSecurity.setDeptId(loginUser.getDeptId());
			newsImportantSecurity.setCreateTime(new Date());
			newsImportantSecurity.setUpdateBy(loginUser.getUsername());
			newsImportantSecurity.setUpdateTime(new Date());
		} else {
			newsImportantSecurity.setUpdateBy(loginUser.getUsername());
			newsImportantSecurity.setUpdateTime(new Date());
		}
		boolean result = newsImportantSecurityService.saveOrUpdate(newsImportantSecurity);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除信息安全国内外要闻信息", notes = "删除信息安全国内外要闻信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsImportantSecurityService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个信息安全国内外要闻信息", notes = "获取单个信息安全国内外要闻信息")
	@GetMapping("/getById")
	public R<NewsImportantSecurityVo> getById(@RequestParam(value = "id") Long id) {
		NewsImportantSecurityQuery newsImportantSecurityQuery = new NewsImportantSecurityQuery();
		newsImportantSecurityQuery.setId(id);
		List<NewsImportantSecurityVo> newsImportantSecurityList = newsImportantSecurityService.getNewsImportantSecurityList(newsImportantSecurityQuery);
		if (newsImportantSecurityList!=null&&newsImportantSecurityList.size()>0) {
			return R.ok(newsImportantSecurityList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取信息安全国内外要闻信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsImportantSecurityVo>> list(@RequestBody NewsImportantSecurityQuery newsImportantSecurityQuery) {
		List<NewsImportantSecurityVo> newsImportantSecurityList = newsImportantSecurityService.getNewsImportantSecurityList(newsImportantSecurityQuery);
		return R.ok(new PageInfo<>(newsImportantSecurityList));
	}
}
